package com.ywl.stream;

import com.ywl.entity.Author;
import com.ywl.entity.Book;
import com.ywl.utils.AuthorFactory;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * Strem流demo01
 *
 * @author 24040
 * @name StreamDemo01
 * @date 2022-08-21 17:40
 */
public class StreamDemo01 {


    public static void main(String[] args) {
        /**
         * 我们可以调用getAuthors方法获取到作家的集合。
         * 现在需要打印所有年龄小于18的作家的名字，并且要注意去重
         */
        List<Author> authors = AuthorFactory.getAuthors();
        Stream<Author> stream = authors.stream();//通过stream来创建流对象
//        test02();
        test03();
    }

    public static void test01(List<Author> authors) {
        authors.stream()//把集合转换为流
                .distinct()
                .filter(author -> author.getAge() < 18)
                .forEach(author -> System.out.println(author.getName()));
    }

    public static void test02() {
        Integer[] arr = {1, 2, 3, 4, 5, 6};
        Stream<Integer> stream = Arrays.stream(arr);//使用Arrays 来操作数组
//        Stream<Integer> arr1 = Stream.of(arr);
        stream.distinct()
                .filter(item -> item > 3)
                .forEach(item -> System.out.print(item));
    }

    public static void test03() {
        Map<String, Integer> map = new HashMap<>();
        map.put("蜡笔小新", 19);
        map.put("黑子", 17);
        map.put("日向翔阳", 16);

        Stream<Map.Entry<String, Integer>> stream = map.entrySet().stream();
        stream.filter(item -> item.getValue() > 16)
                .forEach(item -> System.out.println(item));
    }
}
